/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.aaron.platform.view;

import com.aaron.platform.dao.PageInfo;
import com.aaron.platform.entity.GczxPaymentInfo;
import com.aaron.platform.service.IPaymentMgr;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Properties;
import javax.faces.model.ListDataModel;
import javax.faces.model.SelectItem;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;

/**
 *
 * @author Administrator
 */
public class PaymentListBean extends DataListBean {

    private static Logger logger = Logger.getLogger(PaymentListBean.class);

    /** 付款类型 */
    private int payPro;

    /** 发票编号 */
    private String invoiceNo;

    /** 发票金额 */
    private double innvoiceValue;

    /** 付款日期 */
    private Date payDate;

    /** 供应商 */
    private int enterpreseId;

    /** 合同编号 */
    private String contactCode;

    /** 备注 */
    private String payRemark;

    /** 付款专业 */
    private List<SelectItem> proProList;

    private IPaymentMgr paymentMgr;

    public PaymentListBean() {
        super();
        this.tableTitle = FacesUtil.getString("table_title_paymentlist");

        // 删除、编辑、查看操作
        this.deletable = true;
        this.editable = true;
        this.viewable = true;
        this.multiSelectable = true;

        // 设置分页信息
        pagable = true;
        init();
        
    }

    /**
     * 初始化查询条件
     */
    private void init() {
        payPro = 0;
        innvoiceValue = 0;
        payDate = null;
        enterpreseId = 0;
        contactCode = "";
        invoiceNo = "";
        payRemark = "";
        this.data = new ListDataModel();
        pageInfo = new PageInfo<GczxPaymentInfo>();
    }

    public void reset() {
        init();
    }

    @Override
    public void queryData() {

        // 设置查询条件
        Properties properties = new Properties();
        if (this.payPro != 0) {
            properties.setProperty(GczxPaymentInfo.PAY_PRO, String.valueOf(this.payPro));
        }
        if (this.innvoiceValue != 0) {
            properties.setProperty(GczxPaymentInfo.INVOICE_VALUE, String.valueOf(this.innvoiceValue));
        }
        if (!StringUtils.isEmpty(this.contactCode)) {
            properties.setProperty(GczxPaymentInfo.CONTACT_CODE, StringUtils.trim(this.contactCode));
        }
        if (!StringUtils.isEmpty(this.invoiceNo)) {
            properties.setProperty(GczxPaymentInfo.INVOICE_NO, StringUtils.trim(this.invoiceNo));
        }
        if (this.payDate != null) {
            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
            properties.setProperty(GczxPaymentInfo.PAY_DATE, df.format(payDate));
        }
        if (this.enterpreseId != 0) {
            properties.setProperty(GczxPaymentInfo.ENTERPRISE_ID, String.valueOf(this.enterpreseId));
        }
        if (!StringUtils.isEmpty(this.payRemark)) {
            properties.setProperty(GczxPaymentInfo.PAY_REMARK, String.valueOf(this.payRemark));
        }
        
        pageInfo = paymentMgr.findByCondition(properties, pageInfo.getStartIndex(), pageInfo.getPageSize());
        logger.info("Query payment list finished.");
        data.setWrappedData(pageInfo.getItems());
    }

    @Override
    public void initColumnHeaders() {
        headerColumns.add(new ColumnType(ColumnType.COLUMN_TEXT, "pay_pro",
                FacesUtil.getString("table_column_orderpro"), false, "6%"));
        headerColumns.add(new ColumnType(ColumnType.COLUMN_TEXT, "invoice_no",
                FacesUtil.getString("table_column_invoiceno"), false, "8%"));
        headerColumns.add(new ColumnType(ColumnType.COLUMN_TEXT, "invoice_value",
                FacesUtil.getString("table_column_invoicevalue"), false, "8%"));
        headerColumns.add(new ColumnType(ColumnType.COLUMN_TEXT, "submit_date",
                FacesUtil.getString("table_column_submitdate"), false, "8%"));
        headerColumns.add(new ColumnType(ColumnType.COLUMN_TEXT, "pay_date",
                FacesUtil.getString("table_column_paydate"), false, "8%"));
//        headerColumns.add(new ColumnType(ColumnType.COLUMN_TEXT, "contact_code",
//                FacesUtil.getString("table_column_contactcode"), false, "20%"));
        headerColumns.add(new ColumnType(ColumnType.COLUMN_TEXT, "enterprise_name",
                FacesUtil.getString("table_column_entprisename"), false, "20%"));
        headerColumns.add(new ColumnType(ColumnType.COLUMN_TEXT, "payment_remarks",
                FacesUtil.getString("table_column_payremarks"), false, "20%"));
    }

    @Override
    public String getColumnValue() {
        String columnName = getColumnName();
        increaseColumnIndex();

        GczxPaymentInfo obj = (GczxPaymentInfo) data.getRowData();
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        if (columnName.equals("pay_pro")) {
             return obj.getDisplayPayPro();
        } else if (columnName.equals("invoice_no")) {
            return obj.getInvoiceNo();
        } else if (columnName.equals("invoice_value")) {
            return String.valueOf(obj.getInvoiceValue());
        } else if (columnName.equals("enterprise_name")) {
            return this.getSimpleString(obj.getEnterpriseInfo().getEnterpriseName(),15);
        } else if (columnName.equals("pay_date")) {
            if (obj.getPayDate() == null)
                return "";
            else
                return df.format(obj.getPayDate());
        }
//        } else if (columnName.equals("contact_code")) {
//            if (obj.getPayDetails() != null && obj.getPayDetails().size() > 0) {
//                return obj.getPayDetails().get(0).getOrderInfo().getContactCode();
//            }
//            return "";
        else if (columnName.equals("submit_date")) {
            if (obj.getInvSubdate() == null)
                return "";
            else
                return df.format(obj.getInvSubdate());
        } else if (columnName.equals("payment_remarks")) {
            return this.getSimpleString(obj.getPayRemarks(), 15);
        } 
        else {
            return null;
        }
    }

    @Override
    public String editData() {
        GczxPaymentInfo paymentInfo = (GczxPaymentInfo) data.getRowData();
        PaymentInfoBean  paymentInfoBean = (PaymentInfoBean)FacesUtil.getManagedBean("paymentBean");
        paymentInfoBean.setPaymentInfo(paymentInfo);
        paymentInfoBean.setCurrentOperation(2);
        return "payment_input";
    }

    @Override
    public String showDetail() {
        GczxPaymentInfo paymentInfo = (GczxPaymentInfo) data.getRowData();
        PaymentInfoBean  paymentInfoBean = (PaymentInfoBean)FacesUtil.getManagedBean("paymentBean");
        paymentInfoBean.setPaymentInfo(paymentInfo);
        paymentInfoBean.setCurrentOperation(1);
        return "payment_input";
    }
    
    @Override
    public String deleteData() {
        GczxPaymentInfo paymentInfo = (GczxPaymentInfo) data.getRowData();
        paymentMgr.removePayInfo(paymentInfo.getPayId());
        queryData();
        return null;
    }

    private String getSimpleString(String inStr, int len) {

        int lenStr = inStr.length();
        if (lenStr <= len) {
            return inStr;
        } else {
            StringBuilder sb = new StringBuilder();
            return sb.
                    append(StringUtils.left(inStr, len))
                    .append("...")
                    .toString();
        }
    }

    public String getContactCode() {
        return contactCode;
    }

    public void setContactCode(String contactCode) {
        this.contactCode = contactCode;
    }

    public int getEnterpreseId() {
        return enterpreseId;
    }

    public void setEnterpreseId(int enterpreseId) {
        this.enterpreseId = enterpreseId;
    }

    public double getInnvoiceValue() {
        return innvoiceValue;
    }

    public void setInnvoiceValue(double innvoiceValue) {
        this.innvoiceValue = innvoiceValue;
    }

    public Date getPayDate() {
        return payDate;
    }

    public void setPayDate(Date payDate) {
        this.payDate = payDate;
    }

    public int getPayPro() {
        return payPro;
    }

    public void setPayPro(int payPro) {
        this.payPro = payPro;
    }

    public String getInvoiceNo() {
        return invoiceNo;
    }

    public void setInvoiceNo(String invoiceNo) {
        this.invoiceNo = invoiceNo;
    }

    public String getPayRemark() {
        return payRemark;
    }

    public void setPayRemark(String payRemark) {
        this.payRemark = payRemark;
    }

    public void setPaymentMgr(IPaymentMgr paymentMgr) {
        this.paymentMgr = paymentMgr;
    }

}
